package chapter03;

public class BinarySearch {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        int target = 8;

        int left = 0;
        int right = arr.length - 1;
        int mid = (right + left) / 2;
        int index = -1;

        while(left <= right) {
            if (arr[mid] == target) {
                index = mid;
                break;
            } else if (target > arr[mid]) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
            mid = (right + left) / 2;
        }

        System.out.println(index);
    }
}
